<?php


namespace app\common\model;


use app\common\enum\MainEnum;

class CouponModel extends CommonModel
{
    protected $name = 'coupon';

    public function setAllProductAttr($v): int
    {
        return $v === true ? 1 : 0;
    }

    public function getAllProductAttr($v): bool
    {
        return $v === 1;
    }

    public function setIssueStatusAttr($v): int
    {
        return $v === true ? 1 : 0;
    }

    public function getIssueStatusAttr($v): bool
    {
        return $v === 1;
    }

    //    public function couponInfo(): \think\model\relation\BelongsTo
    //    {
    //        return $this->belongsTo(MediaModel::class, 'media_id', 'id');
    //    }

    public static function getExtendTypeTextAttr($v, $data): ?string
    {
        foreach (MainEnum::COUPON_TYPE as $key => $item) {
            if ($item === $data['type']) {
                return $key;
            }
        }
        return '';
    }

    public function getUseMinPriceAttr($value): float
    {
        return (float)$value;
    }

    public function getTypeJianPriceAttr($value): float
    {
        return (float)$value;
    }

    public function getTypeDiscountPriceAttr($value): float
    {
        return (float)$value / 100;
    }
//
//    public function getTypeDiscountPriceTextAttr($value): float
//    {
//        return (float)$value / 10;
//    }

    public function getTypeRandomMinPriceAttr($value): float
    {
        return (float)$value;
    }

    public function getTypeRandomMaxPriceAttr($value): float
    {
        return (float)$value;
    }

    public function getExtendDescAttr($v, $data): ?string
    {
        $str = '';
        if ((float)$data['use_min_price'] !== 0) {
            $str .= '满' . (float)$data['use_min_price'] . '元';
        }
        switch ($data['type']) {
            case 1:
                #满减
                return $str . '减' . (float)$data['type_jian_price'] . '元';
            case 2:
                #折扣
                return $str . '打' . (float)($data['type_discount_price'] / 10) . '折';
            case 3:
                #随机减
                return $str . '随机减' . (float)$data['type_random_min_price'] . '~' . (float)$data['type_random_max_price'] . '元';
            default:
                return '';
        }
    }

    public function productsInfo(): \think\model\relation\BelongsToMany
    {
        return $this->belongsToMany(ProductModel::class, CouponProductModel::class, 'product_id', 'coupon_id');
    }

    public function products(): \think\model\relation\hasMany
    {
        return $this->hasMany(CouponProductModel::class, 'coupon_id', 'id');
    }

    public function getCoupon($where = [])
    {
        return $this->with([
            'productsInfo',
        ])->order([
            'create_time' => 'desc',
        ])->where($where)->append([
            'extend_desc',
            'extend_type_text',
        ])->find();
    }

    public function getCouponList($user_id)
    {
        return $this->with([
            'productsInfo',
        ])->order([
            'create_time' => 'desc',
        ])->where('user_id', $user_id)->append([
            'extend_desc',
            'extend_type_text',
        ])->select();
    }

    public function getCouponListPaginate($where = [], $page = 1, $limit = 10)
    {
        return $this->with([
            'productsInfo',
        ])->order([
            'create_time' => 'desc',
        ])->where($where)->append([
            'extend_desc',
            'extend_type_text',
        ])->paginate([
            'page'      => $page,
            'list_rows' => $limit,
        ]);
    }

    public function getCouponIssueList()
    {
        return $this->with([])
            ->order([
                'create_time' => 'desc',
            ])
            ->append([
                'extend_desc',
                'extend_type_text',
            ])
            ->where('issue_status', MainEnum::COUPON_ISSUE_STATUS['已开启'])
            ->whereTime('issue_start_time', '<', NOW_TIME)
            ->whereTime('issue_end_time', '>', NOW_TIME)
            ->select();
    }
}